;(function($){
	$.th_search = $.th_search || {}
	$.fn.th_search = function(opts){
		var el = this;
		var opt = {
			top:"50px",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.th_search = $.extend($.th_search,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//清除
			el.find("#btnClear").unbind("click").click(function(){
				el.find("#thSearch").val("")
			})
			
			//查询
			el.find("#btnSearch").unbind("click").click(function(event,value){
				t.triggerClick();
				var value = el.find("#thSearch").val()
				param.dataBack(value)
			})
			
			//输入手牌
			el.find("#thSearch").unbind("keyup").keyup(function(){
				if($(this).val().length==4){
					el.find("#btnSearch").trigger("click")
				}
			})
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var csstext = 	'<style>'+
								'.ace-settings-container {position: absolute !important;left: 0 !important;right:auto !important;top: '+param.top+' !important;z-index: 12 !important;}'+
								'.btn.btn-app.ace-settings-btn {float: left !important;display: block !important;text-align: center !important;border-radius: 0px 6px 6px 0px !important;opacity: 0.55 !important;vertical-align: top !important;margin: 0 !important;padding-bottom: 10px !important;}'+
								'#ace-settings-box{width: 300px;padding: 0px !important;}'+
							'</style>';
			$(csstext).appendTo(el)
			
			el.find("#ace-settings-container").remove()
			var html = 	'<div class="ace-settings-container" id="ace-settings-container">'+
							'<div class="ace-settings-box clearfix" id="ace-settings-box" style="border:0;">'+
								'<div class="widget-box widget-color-orange no-margin">'+
									'<div class="widget-header">'+
										'<h5 class="widget-title bigger lighter">查询手牌</h5>'+
									'</div>'+
					
									'<div class="widget-body">'+
										'<div class="widget-main" style="height: 350px;text-align: center;">'+
											'<input type="text" id="thSearch" name="thSearch" class="" style="height: 39px;width: 225px;font-size: 20px;">'+
											'<div class="btn-group" style="margin-top:-7px;">'+
												'<button id="btnClear" class="btn btn-normal btn-info" style="padding:0px 12px">'+
													'<span class="" style="font-size: 24px;font-weight: bold;">C</span>'+
												'</button>'+
											'</div>'+
											'<hr style="margin:10px 0px">'+
											'<div id="numberLetters">'+
											'</div>'+
										'</div>'+
					
										'<div>'+
											'<a  class="btn btn-block btn-warning btnNumber" style="width:50%;margin:0;">'+
												'<span>字母</span>'+
											'</a>'+
											'<a  class="btn btn-block btn-warning btnNumber" style="width:50%;margin:0;display:none">'+
												'<span>数字</span>'+
											'</a>'+
											'<a id="btnSearch"  class="btn btn-block btn-primary" style="width:50%;margin:0;background-color: #ff9808 !important;border-color: #ff9808;">'+
												'<i class="ace-icon fa fa-search bigger-110"></i>'+
												'<span>搜索</span>'+
											'</a>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="btn btn-app btn-xs btn-warning ace-settings-btn" id="ace-settings-btn">'+
								'<i class="ace-icon fa fa-search bigger-130" style="line-height: 22px;"></i>'+
							'</div>'+
					
						'</div>';
			el.append(html)
			
			$.ajaxSetup({async: false,cache: false});
            $.getScript("assets/js/ace/ace.settings.js");
            $.getScript("assets/js/ace/ace.settings-rtl.js");
            $.getScript("assets/js/ace/ace.settings-skin.js");
            $.ajaxSetup({async: true});
            
			t.init();
		},
		init:function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			if(!$.isFunction($().number_letters_keyboard)){
				$.ajaxSetup({async: false,cache: false});
	            $.getScript("js/spa_guestList/jquery.service.number_letters_keyboard.js");
	            $.ajaxSetup({async: true});
			}
			el.find("#numberLetters").number_letters_keyboard({
				dataBack : function(value){
					value = $("#thSearch").val()+value
					$("#thSearch").val(value);
					if(value.toString().length==4){
						t.triggerClick();
						param.dataBack(value)
						$("#thSearch").val("");
//						$("#btnSearch").trigger("click")
					}
				}
			})
			
			$(".number-letters-item").addClass("hide")
			$(".btnNumber").unbind("click").click(function(){
				$(".btnNumber").toggle();
				$(".number-letters-item:eq(0)").trigger("click");
			})
			
		},
		triggerClick:function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.find("#ace-settings-btn").trigger("click")
		},
		toFocus:function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.find("#thSearch").focus()
		}
	}
})(jQuery);